package liurong.driver.line.count;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 2017-08-23
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:10443行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:676行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:79209行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:3835行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:16532行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:792行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:394行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:8516行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：133303
 * 
 * 
 * 2018.1.23
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:11711行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2655行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:88313行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:6379行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:18906行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1326行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1083行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1733行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2339行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:9702行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1131行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：158743
 * 
 * 2018.05.16
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:12912行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2677行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:90178行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:7102行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:19595行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1365行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1722行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2343行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:9997行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：163641
 * 
 * 
 * 2018.06.30
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:13660行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2677行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:92094行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:8329行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:20343行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1365行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1750行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2343行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:9997行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：168308
 * 
 * 
 * 2018.10.10
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:14210行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2769行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:97958行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:9411行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:20835行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1365行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1750行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2606行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:10163行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：176817
 * 
 * 
 * 2018.10.19
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:14234行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2769行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:98514行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:9419行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:20842行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1365行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1750行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2606行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:10163行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：177412
 * 
 * 
 * 
 * 2018.12.15
 * 
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:14461行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2779行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:99680行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:9751行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:21190行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1366行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1750行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2606行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:10163行
 * D:\workspace\ez_boss\ez-open-api/src/main/java Totle:987行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：180483
 * 
 * D:\workspace\ez_boss\ez-activity/src/main/java Totle:77行
 * D:\workspace\ez_boss\ez-api/src/main/java Totle:14820行
 * D:\workspace\ez_boss\ez-common-utils/src/main/java Totle:2831行
 * D:\workspace\ez_boss\ez-core/src/main/java Totle:102255行
 * D:\workspace\ez_boss\ez-core-api/src/main/java Totle:10835行
 * D:\workspace\ez_boss\ez-core-model/src/main/java Totle:22085行
 * D:\workspace\ez_boss\ez-ea/src/main/java Totle:1366行
 * D:\workspace\ez_boss\ez-ea-api/src/main/java Totle:1143行
 * D:\workspace\ez_boss\ez-email/src/main/java Totle:1750行
 * D:\workspace\ez_boss\ez-email-api/src/main/java Totle:559行
 * D:\workspace\ez_boss\ez-local-api/src/main/java Totle:2606行
 * D:\workspace\ez_boss\ez-oms/src/main/java Totle:10163行
 * D:\workspace\ez_boss\ez-oms-api/src/main/java Totle:1583行
 * D:\workspace\ez_boss\ez-open-api/src/main/java Totle:1310行
 * D:\workspace\ez_boss\ez-pms/src/main/java Totle:1142行
 * D:\workspace\ez_boss\ez-uc/src/main/java Totle:12906行
 * 
 * ez-boss的总行数为：187431
 * 
 * 
 * @author liurong
 *
 */
public class LineCounter {
	List<File> list = new ArrayList<File>();
	int linenumber = 0;

	FileReader fr = null;
	BufferedReader br = null;

	public void counter(String projectName) {
		File projectFile = new File(projectName);
		File[] projectListFiles = projectFile.listFiles();
		int totalLine = 0;
		for (File file : projectListFiles) {
			if (file.isDirectory()) {
				String path = file.getAbsolutePath() + "/src/main/java";
				File file1 = new File(path);
				if (!file1.exists()) {
					continue;
				}
				System.out.println(path);
				File files[] = null;
				files = file1.listFiles();
				addFile(files);
				isDirectory(files);
				readLinePerFile();
				System.out.println("Totle:" + linenumber + "行");
			}
			totalLine = totalLine + linenumber;
			list.clear();
			linenumber = 0;
		}
		System.out.println();
		System.out.println("ez-boss的总行数为：" + totalLine);
	}

	// 判断是否是目录
	public void isDirectory(File[] files) {
		for (File s : files) {
			if (s.isDirectory()) {
				File file[] = s.listFiles();
				addFile(file);
				isDirectory(file);
				continue;
			}
		}
	}

	// 将src下所有文件组织成list
	public void addFile(File file[]) {
		for (int index = 0; index < file.length; index++) {
			list.add(file[index]);
			// System.out.println(list.size());
		}
	}

	// 读取非空白行
	public void readLinePerFile() {
		try {
			for (File s : list) {
				if (s.isDirectory()) {
					continue;
				}
				fr = new FileReader(s);
				br = new BufferedReader(fr);
				String i = "";
				while ((i = br.readLine()) != null) {
					if (isBlankLine(i) && isNote(i))
						linenumber++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
				}
			}
			if (fr != null) {
				try {
					fr.close();
				} catch (Exception e) {
				}
			}
		}
	}

	private boolean isNote(String i) {
		String trim = i.trim();
		if (trim.startsWith("/") || trim.endsWith("/") || trim.startsWith("*")) {
			return false;
		} else {
			return true;
		}
	}

	// 是否是空行
	public boolean isBlankLine(String i) {
		if (i.trim().length() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public static void main(String args[]) {
		LineCounter lc = new LineCounter();
		String projectName = "D:\\workspace\\yunji-omsorder"; // 这里传入你的项目名称
		lc.counter(projectName);
	}
}